import React, {useEffect, useState} from 'react'
import {Button, Space, message, Popconfirm, Tag} from 'antd'

import type {ColumnsType} from 'antd/es/table'
import {history} from 'umi'
import styles from './index.less'
import SearchForm from '@/components/SearchForm'
import CustomTable from '@/components/Table'
import Permissions from '@/components/Permissions'
import {getTableList, delectDict} from '@/service/tool/dict'

import {formatDate} from '@/utils/common/dateFilter'
import 'moment/locale/zh-cn'

const formItemLayout = {
	labelCol: {flex: '80px'},
	wrapperCol: {flex: '1'},
}

const dict = () => {
	const [params, setParams] = useState({
		current: 1,
		size: 10,
	})
	const [total, setTotal] = useState()
	const [loading, setLoading] = useState(false)
	const [tableList, setTableList] = useState<any[]>([])
	const [searchData, setSearchData] = useState<any>({})
	const [formData, setFormData] = useState<any[]>([])

	// 获取表格数据
	const onListFun = () => {
		setLoading(true)
		getTableList({...params, ...searchData})
			.then((res: any) => {
				if (res?.success) {
					setTableList(() => res?.data?.records)
					setTotal(res?.data?.total)
					setLoading(false)
				}
			})
			.catch(() => {
				setLoading(false)
			})
	}
	useEffect(() => {
		const formItems: any[] = [
			{
				type: 'group',
				key: 'group1',
				groupItems: [
					{
						type: 'input',
						label: '字典编号',
						name: 'type',
						key: 'type',
					},
					{
						type: 'input',
						label: '字典名称',
						name: 'valueName',
						key: 'valueName',
					},
					{},
				],
			},
		]
		setFormData(formItems)
	}, [])

	useEffect(() => {
		// 表格查询
		onListFun()
	}, [searchData, params])

	// 删除事件
	const onDeleteFun = (id: string) => {
		delectDict({id})
			.then((res) => {
				if (res?.success) {
					message.success(res.msg)
					// setIsModalOpen(() => false);
					onListFun()
				}
			})
			.catch((err) => {
				message.error(err)
			})
	}

	const columns: ColumnsType<InitiationAPI.InitiationColumns> = [
		{
			title: '字典名称',
			dataIndex: 'valueName',
			align: 'center',
			render: (text: any) => <span>{text || '--'}</span>,
		},
		{
			title: '字典编号',
			dataIndex: 'type',
			align: 'center',
			render: (text: any, record: any) => (
				<Button
					type="link"
					size="small"
					onClick={() => {
						history.push({
							pathname: '/tool/dict/list',
							query: {
								id: record.id,
								type: record.type,
							},
						})
					}}>
					{text || '--'}
				</Button>
			),
		},
		{
			title: '字典状态',
			dataIndex: 'status',
			align: 'center',
			render: (text: any) => (
				<>
					{text === 1 ? (
						<Tag color="processing">有效</Tag>
					) : (
						<Tag color="processing">无效</Tag>
					)}
				</>
			),
		},
		{
			title: '修改时间',
			dataIndex: 'modified',
			align: 'center',
			render: (text: any) => <span>{text || '--'}</span>,
		},
		{
			title: '字典描述',
			dataIndex: 'description',
			align: 'center',
			render: (text: any) => <span>{text || '--'}</span>,
		},
		{
			title: '操作',
			key: 'action',
			align: 'center',
			fixed: 'right',
			width: 250,
			render: (text: any, record: any) => (
				<div>
					<Permissions permissionID="amc_system_dict_view">
						<Button
							type="link"
							size="small"
							onClick={() => {
								history.push({
									pathname: '/tool/dict/view',
									query: {
										id: record.id,
									},
								})
							}}>
							详情
						</Button>
					</Permissions>
					<Permissions permissionID="amc_system_dict_edit">
						<Button
							type="link"
							size="small"
							onClick={() => {
								history.push({
									pathname: '/tool/dict/edit',
									query: {
										id: record.id,
										type: record.type,
									},
								})
							}}>
							编辑
						</Button>
					</Permissions>
					<Permissions permissionID="amc_system_dict_delete">
						<Popconfirm
							placement="topLeft"
							title="你确定要删除这条字典数据吗？"
							onConfirm={() => onDeleteFun(record.id)}
							okText="确定"
							cancelText="取消">
							<Button type="link" size="small">
								删除
							</Button>
						</Popconfirm>
					</Permissions>
				</div>
			),
		},
	]

	const onFinish = (values: any) => {
		const applyTimeStart = formatDate(values?.applyTime?.[0], 'YYYY-MM-DD')
		const applyTimeEnd = formatDate(values?.applyTime?.[1], 'YYYY-MM-DD')
		const search = {
			...values,
			budgetMix: values?.budgetMix,
			budgetMax: values?.budgetMax,
			applyTimeStart,
			applyTimeEnd,
		}
		setSearchData(() => search)
	}

	// 搜索重置
	const onReset = () => {
		setSearchData({})
	}

	return (
		<div className={styles.page}>
			<div className="search">
				{formData && (
					<SearchForm
						onFinish={onFinish}
						onReset={onReset}
						formItems={formData}
						formLayout={formItemLayout}
					/>
				)}
			</div>
			<div className="btnlist">
				<Space size={12}>
					<Permissions permissionID="amc_system_dict_add">
						<Button
							type="primary"
							ghost
							size="small"
							onClick={() => {
								history.push('/tool/dict/add')
							}}>
							新增
						</Button>
					</Permissions>
				</Space>
			</div>
			<div className="table">
				<CustomTable
					params={params}
					columns={columns}
					total={total}
					pagination
					scroll={{y: 500}}
					callback={(pagination: any) => {
						setParams(pagination)
					}}
					loading={loading}
					dataSource={tableList}
				/>
			</div>
		</div>
	)
}
export default dict
